from conduit.datype import ConduitTypeBase


class ConduitBase(object):
    "管道基础类"
    data_type = None  # 类型CLass
    name = None  # 名字STR

    def __init__(self, redis):
        self.redis = redis  # 一个redis链接

    def conduit(self, data: data_type) -> data_type or None:
        "用户逻辑"
        pass

    def process(self, data: bytes):
        "进程控制"
        pass

    def transition(self, data: str):
        if issubclass(ConduitTypeBase, self.data_type):  # 用户定义
            useful = self.data_type.transition(data)
        elif isinstance(self.data_type, int):
            useful = int(data)
        elif isinstance(self.data_type, str):
            return data
        elif isinstance(self.data_type, float):
            useful = float(data)
        else:
            raise Exception("类型错误，无法处理这个类型")  # 抛出错误

        return useful


class InitConduitBase(object):
    data_type = None  # 类型CLass

    def __init__(self, redis):
        self.redis = redis  # 一个redis链接

    def conduit(self) -> data_type:
        "用户逻辑"
        pass

    def process(self, data: bytes):
        "进程控制"
        pass

    def transition(self, data: str):
        if issubclass(ConduitTypeBase, self.data_type):  # 用户定义
            useful = self.data_type.transition(data)
        elif isinstance(self.data_type, int):
            useful = int(data)
        elif isinstance(self.data_type, str):
            return data
        elif isinstance(self.data_type, float):
            useful = float(data)
        else:
            raise Exception("类型错误，无法处理这个类型")  # 抛出错误

        return useful
